package common;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * @author ever
 */
public class ReadConf {

    /**
     * classpath 项目配置文件
     */
    private final String classpath = ".classpath";

    private final String setting = ".settings" + File.separator + "org.eclipse.wst.common.component";

    /**
     * 用户桌面
     */
    private final String desktop = "Desktop";

    private List<Component> srcList = new ArrayList<Component>(16);


    public String webRoot = "";

    private String proName;

    private String output = "WebRoot/WEB-INF/classes";

    public String getOutput() {
        return output;
    }


    /**
     * 根据项目中的.classpath,获取'src'和文件编译路径,
     * rone中有多个src,所以结果用list
     *
     * @return
     */
    public void readProjectConf() {
        String path = Constant.workSpacePath + File.separator + classpath;
        File file = new File(path);
        // 创建saxReader对象
        SAXReader reader = new SAXReader();
        // 通过read方法读取一个文件 转换成Document对象
        Document document = null;
        try {
            document = reader.read(file);
        } catch (DocumentException e) {
            System.out.println(e.getMessage());
        }
        //获取根节点元素对象
        Element root = document.getRootElement();
        Iterator<Element> iterator = root.elementIterator();
        while (iterator.hasNext()) {
            Element next = iterator.next();
            if ("src".equals(next.attributeValue("kind"))) {
                srcList.add(new Component(next.attributeValue("output"), next.attributeValue("path")));
            }
            if ("output".equals(next.attributeValue("kind"))) {
                output = next.attributeValue("path");
            }
        }

//        getWebRoot();
    }

    /**
     * 根据 org.eclipse.wst.common.component中的deploy-name 获取项目名称
     */
    public void getWebRoot() {
        String path = Constant.workSpacePath + File.separator + setting;
        File file = new File(path);
        // 创建saxReader对象
        SAXReader reader = new SAXReader();
        // 通过read方法读取一个文件 转换成Document对象
        Document document = null;
        try {
            document = reader.read(file);
            if (document == null) {
                return;
            }
        } catch (DocumentException e) {
            System.out.println(e.getMessage());
        }
        //获取根节点元素对象
        Element root = document.getRootElement();
        Element wbModule = (Element) root.elementIterator().next();
        this.proName = wbModule.attributeValue("deploy-name");

        Iterator<Element> iterator = wbModule.elementIterator();
        while (iterator.hasNext()) {
            Element next = iterator.next();
            if ("/".equals(next.attributeValue("deploy-path"))) {
                webRoot = next.attributeValue("source-path").substring(1);
                break;
            }
        }
    }


    public String getOutPutPath() {
        if (StringUtils.isNotBlank(Constant.outputPath)) {
            return Constant.outputPath;
        }
        Map<String, String> system = System.getenv();
        return system.get("USERPROFILE") + File.separator + File.separator + desktop;
    }

    public ReadConf() {
        this.readProjectConf();
        this.getOutPutPath();
        this.getWebRoot();
    }


    public String getProName() {
        return proName;
    }


    public List<Component> getSrcList() {
        return srcList;
    }


}
